package cn.iocoder.yudao.module.infra.service.ocr;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.record.OcrRecordPageReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.ocr.OcrRecordDO;
import cn.iocoder.yudao.module.infra.dal.mysql.ocr.OcrRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.OCR_RECORD_NOT_EXISTS;

/**
 * OCR Record Service Implementation
 *
 * @author YuDao
 */
@Service
@Validated
@Slf4j
public class OcrRecordServiceImpl implements OcrRecordService {

    @Resource
    private OcrRecordMapper ocrRecordMapper;

    @Resource
    private OcrConfigService ocrConfigService;

    @Override
    public Long createOcrRecord(OcrRecordDO record) {
        ocrRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void updateOcrRecord(OcrRecordDO record) {
        // Validate existence
        validateOcrRecordExists(record.getId());
        // Update OCR record
        ocrRecordMapper.updateById(record);
    }

    @Override
    public void deleteOcrRecord(Long id) {
        // Validate existence
        validateOcrRecordExists(id);
        // Delete OCR record
        ocrRecordMapper.deleteById(id);
    }

    @Override
    public OcrRecordDO getOcrRecord(Long id) {
        return ocrRecordMapper.selectById(id);
    }

    @Override
    public OcrRecordDO getOcrRecordByFileId(Long fileId) {
        return ocrRecordMapper.selectByFileId(fileId);
    }

    @Override
    public PageResult<OcrRecordDO> getOcrRecordPage(OcrRecordPageReqVO pageReqVO) {
        PageResult<OcrRecordDO> pageResult = ocrRecordMapper.selectPage(pageReqVO);

        // 填充配置名称和其他字段
        if (pageResult.getList() != null && !pageResult.getList().isEmpty()) {
            Set<Long> configIds = pageResult.getList().stream()
                    .map(OcrRecordDO::getConfigId)
                    .collect(Collectors.toSet());

            Map<Long, String> configNameMap = ocrConfigService.getOcrConfigNameMap(configIds);

            pageResult.getList().forEach(record -> {
                record.setConfigName(configNameMap.get(record.getConfigId()));
                record.setImageUrl(record.getFileUrl()); // 设置图片URL
            });
        }

        return pageResult;
    }

    @Override
    public void deleteOcrRecordList(java.util.List<Long> ids) {
        // Delete OCR records by IDs
        ocrRecordMapper.deleteByIds(ids);
    }

    private OcrRecordDO validateOcrRecordExists(Long id) {
        OcrRecordDO record = ocrRecordMapper.selectById(id);
        if (record == null) {
            throw exception(OCR_RECORD_NOT_EXISTS);
        }
        return record;
    }

}